using System;
using System.IO;
using HPA.SQL;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
namespace HPA.Common
{
    #region CImageUtility
    public class CImageUtility
    {
        public static System.Drawing.Image byteArrayToImage(byte[] byteArrayIn)
        {
            MemoryStream ms = new MemoryStream(byteArrayIn);
            System.Drawing.Image returnImage = System.Drawing.Image.FromStream(ms);
            return returnImage;
        }
        public static byte[] ImageToByte(System.Drawing.Image img)
        {
            MemoryStream ms = new MemoryStream();
            img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            return ms.ToArray();
        }
        public static byte[] ImageToByte(System.Drawing.Bitmap img)
        {
            MemoryStream ms = new MemoryStream();
            img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            return ms.ToArray();
        }
        public static bool readFile(string FileName, out byte[] Picture)
        {
            byte[] buffer = null;
            if (!File.Exists(FileName))
            {
                Picture = null;
                return false;
            }

            FileStream fs = null;
            try
            {
                #region Reading file

                fs = new FileStream(FileName, FileMode.Open, FileAccess.Read);

                buffer = new byte[fs.Length];
                //
                // Reading the content of the file into an array of bytes.
                //
                fs.Read(buffer, 0, Convert.ToInt32(fs.Length));

                fs.Close();
                // ------------------------------------------				
                #endregion

                Picture = buffer;
                return true;
            }
            catch (Exception)
            {
                Picture = null;
                return false;
            }

        }
        /// <summary> 
        /// A quick lookup for getting image encoders 
        /// </summary> 
        private static Dictionary<string, ImageCodecInfo> encoders = null;

        /// <summary> 
        /// A quick lookup for getting image encoders 
        /// </summary> 
        public static Dictionary<string, ImageCodecInfo> Encoders
        {
            //get accessor that creates the dictionary on demand 
            get
            {
                //if the quick lookup isn't initialised, initialise it 
                if (encoders == null)
                {
                    encoders = new Dictionary<string, ImageCodecInfo>();
                }

                //if there are no codecs, try loading them 
                if (encoders.Count == 0)
                {
                    //get all the codecs 
                    foreach (ImageCodecInfo codec in ImageCodecInfo.GetImageEncoders())
                    {
                        //add each codec to the quick lookup 
                        encoders.Add(codec.MimeType.ToLower(), codec);
                    }
                }

                //return the lookup 
                return encoders;
            }
        }

        /// <summary> 
        /// Resize the image to the specified width and height. 
        /// </summary> 
        /// <param name="image">The image to resize.</param> 
        /// <param name="width">The width to resize to.</param> 
        /// <param name="height">The height to resize to.</param> 
        /// <returns>The resized image.</returns> 
        public static System.Drawing.Bitmap ResizeImage(System.Drawing.Image image, int width, int height)
        {
            //a holder for the result 
            Bitmap result = new Bitmap(width, height);

            //use a graphics object to draw the resized image into the bitmap 
            using (Graphics graphics = Graphics.FromImage(result))
            {
                //set the resize quality modes to high quality 
                graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                //draw the image into the target bitmap 
                graphics.DrawImage(image, 0, 0, result.Width, result.Height);
            }

            //return the resulting bitmap 
            return result;
        }

        /// <summary>  
        /// Saves an image as a jpeg image, with the given quality  
        /// </summary>  
        /// <param name="path">Path to which the image would be saved.</param>  
        /// <param name="quality">An integer from 0 to 100, with 100 being the  
        /// highest quality</param>  
        /// <exception cref="ArgumentOutOfRangeException"> 
        /// An invalid value was entered for image quality. 
        /// </exception> 
        public static void SaveJpeg(string path, Image image, int quality)
        {
            //ensure the quality is within the correct range 
            if ((quality < 0) || (quality > 100))
            {
                //create the error message 
                string error = string.Format("Jpeg image quality must be between 0 and 100, with 100 being the highest quality.  A value of {0} was specified.", quality);
                //throw a helpful exception 
                throw new ArgumentOutOfRangeException(error);
            }

            //create an encoder parameter for the image quality 
            EncoderParameter qualityParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
            //get the jpeg codec 
            ImageCodecInfo jpegCodec = GetEncoderInfo("image/jpeg");

            //create a collection of all parameters that we will pass to the encoder 
            EncoderParameters encoderParams = new EncoderParameters(1);
            //set the quality parameter for the codec 
            encoderParams.Param[0] = qualityParam;
            //save the image using the codec and the parameters 
            image.Save(path, jpegCodec, encoderParams);
        }

        /// <summary>  
        /// Returns the image codec with the given mime type  
        /// </summary>  
        public static ImageCodecInfo GetEncoderInfo(string mimeType)
        {
            //do a case insensitive search for the mime type 
            string lookupKey = mimeType.ToLower();

            //the codec to return, default to null 
            ImageCodecInfo foundCodec = null;

            //if we have the encoder, get it to return 
            if (Encoders.ContainsKey(lookupKey))
            {
                //pull the codec from the lookup 
                foundCodec = Encoders[lookupKey];
            }

            return foundCodec;
        }
    }
    #endregion
}
